//
//  EditViewController.m
//  Demo0005
//
//  Created by zTercel on 13-6-23.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import "EditViewController.h"

@interface EditViewController ()

@end

@implementation EditViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

	int screenWidth = [[UIScreen mainScreen] bounds].size.width;

	UILabel	*nameLabel			= [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 60, 33)];
	nameLabel.text				= @"用户名:";
	nameLabel.textAlignment		= NSTextAlignmentRight;
	nameLabel.backgroundColor	= [UIColor lightGrayColor];
	[self.view addSubview:nameLabel];
	[nameLabel release];
	
	UITextField *nameField		= [[UITextField alloc] initWithFrame:CGRectMake(100, 150, 200, 33)];
	nameField.borderStyle		= UITextBorderStyleBezel;
	nameField.keyboardType		= UIKeyboardTypeASCIICapable;
	nameField.returnKeyType		= UIReturnKeyNext;
	nameField.tag				= 101;
	nameField.clearButtonMode	= UITextFieldViewModeWhileEditing;
	[nameField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[nameField addTarget:self action:@selector(nameFieldDidEditOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
	[self.view addSubview:nameField];
	[nameField becomeFirstResponder];
	[nameField release];
	
	UILabel	*emailLabel			= [[UILabel alloc] initWithFrame:CGRectMake(20, 210, 60, 33)];
	emailLabel.text				= @"e-Mail:";
	emailLabel.textAlignment	= NSTextAlignmentRight;
	emailLabel.backgroundColor	= [UIColor lightGrayColor];
	[self.view addSubview:emailLabel];
	[emailLabel release];
	
	UITextField *emailField		= [[UITextField alloc] initWithFrame:CGRectMake(100, 210, 200, 33)];
	emailField.borderStyle		= UITextBorderStyleBezel;
	emailField.keyboardType		= UIKeyboardTypeEmailAddress;
	emailField.returnKeyType	= UIReturnKeyDone;
	emailField.tag				= 102;
	emailField.clearButtonMode	= UITextFieldViewModeWhileEditing;
	[emailField addTarget:self action:@selector(emailFieldDidEditOnExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
	[emailField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
	[self.view addSubview:emailField];
	[emailField release];
	
	UIButton *enterButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	[enterButton setFrame: CGRectMake((screenWidth - 100)/2, 280, 100, 40)];
	[enterButton setTitle: @"确定" forState: UIControlStateNormal];
	[enterButton addTarget:self action:@selector(enterButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:enterButton];
	
	[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateUserInfo:) name:kUserInfo object:nil];
}

- (void)updateUserInfo:(NSNotification *)notification
{
	NSDictionary *userInfo = notification.object;
	if ([userInfo count] > 0) {
		NSEnumerator *enumerator = [userInfo keyEnumerator];
		for (NSString *key in enumerator) {
			if (NSOrderedSame == [key compare:@"name" options:NSCaseInsensitiveSearch]) {
				UITextField *nameField = (UITextField*)[self.view viewWithTag:101];
				nameField.text	= userInfo[key];
			}
			if (NSOrderedSame == [key compare:@"email" options:NSCaseInsensitiveSearch]) {
				UITextField *emailField	= (UITextField*)[self.view viewWithTag:102];
				emailField.text = [userInfo valueForKey:key];
			}
		}
	}
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 
#pragma mark 确定按钮消息
- (void)enterButtonTapped:(id)sender
{
	UITextField *nameField = (UITextField*)[self.view viewWithTag:101];
	UITextField *emailField	= (UITextField*)[self.view viewWithTag:102];
	
	if ([self.delegate respondsToSelector:@selector(ChangeNameValue:forEMail:)]) {
		[self.delegate ChangeNameValue:nameField.text forEMail:emailField.text];
	}
	
	[self dismissViewControllerAnimated:YES completion:^{
		[[NSNotificationCenter defaultCenter]removeObserver:self name:kUserInfo object:nil];
	}];
}

#pragma mark nameFiled中键盘操作消息
- (IBAction)nameFieldDidEditOnExit:(id)sender
{
	UITextField *emailField	= (UITextField*)[self.view viewWithTag:102];
	[emailField becomeFirstResponder];
}

#pragma mark emailField中键盘操作消息
- (IBAction)emailFieldDidEditOnExit:(id)sender
{
	[sender resignFirstResponder];
}

#pragma mark 用户点击视图消息
- (IBAction)tapView:(id)sender {
	[[UIApplication sharedApplication]sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
@end
